home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 24 / Amiga Format AFCD24 (Feb 1998, Issue 108).iso / -seriously_amiga- / shareware / programming / other / pmdev / demos / menuverify.c < prev    next >
C/C++ Source or Header  |  1998-01-05  |  4KB  |  114 lines

  1. //
  2. // $VER: MenuVerify.c 1.0 (12.4.97)
  3. //
  4. // Popup Menu example program
  5. //
  6. // ©1996-1997 Henrik Isaksson
  7. // All Rights Reserved.
  8. //
  9. // Run and click the mouse in the window!
  10. //
  11.  
  12. #include <intuition/intuition.h>
  13.  
  14. #include <clib/intuition_protos.h>
  15. #include <clib/exec_protos.h>
  16. #include <clib/alib_protos.h>
  17.  
  18. #include <string.h>
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21.  
  22. #include <libraries/pm.h>
  23. #include <proto/pm.h>
  24.  
  25. struct IntuitionBase    *IntuitionBase;
  26. struct GfxBase        *GfxBase;
  27. struct PopupMenuBase    *PopupMenuBase;
  28.  
  29. struct Window *w;    // This window is only needed to find out when and where the menu should appear.
  30.             // The font in this window's rastport will be used for the menu.
  31.  
  32. // Just a badly coded (hardcoded) intuition menu....
  33. struct IntuiText text1 = { 2, 1, 1, 1, 1, 0L, "First Item", 0L };
  34. struct IntuiText text2 = { 2, 1, 1, 1, 1, 0L, "Last Item", 0L };
  35. struct MenuItem Last_Item = { NULL, 0, 15, 100, 10, ITEMENABLED|ITEMTEXT|HIGHCOMP, 0L, &text2, 0L, 0, 0L, 0 };
  36. struct MenuItem First_Item = { &Last_Item, 0, 0, 100, 10, ITEMENABLED|ITEMTEXT|HIGHCOMP, 0L, &text1, 0L, 0, 0L, 0 };
  37. struct Menu Intui_Menu = { NULL, 0, 0, 100, 12, MENUENABLED, "Project", &First_Item, 0, 0, 0, 0};
  38.  
  39. void main()
  40. {
  41.     BOOL r=TRUE, menu=FALSE;
  42.     struct IntuiMessage *im,imsg;
  43.     struct PopupMenu *p;
  44.  
  45.     PopupMenuBase=(struct PopupMenuBase *)OpenLibrary(POPUPMENU_NAME,POPUPMENU_VERSION);            // Open the library
  46.     if(PopupMenuBase) {
  47.         IntuitionBase=(struct IntuitionBase *)PopupMenuBase->pmb_IntuitionBase;    // We let popupmenu.library open the libraries we need
  48.         GfxBase=(struct GfxBase *)PopupMenuBase->pmb_GfxBase;            // They remain valid until the library is closed!
  49.  
  50.         p=PMMenu("Inside the window!"),    // Create a very simple menu...
  51.             PMItem("This example shows"),    PM_NoSelect,    TRUE,    PMEnd,
  52.             PMItem("how to use popup"),    PM_NoSelect,    TRUE,    PMEnd,
  53.             PMItem("menus and intuition"),    PM_NoSelect,    TRUE,    PMEnd,
  54.             PMItem("menus in the same"),    PM_NoSelect,    TRUE,    PMEnd,
  55.             PMItem("window!"),        PM_NoSelect,    TRUE,    PMEnd,
  56.             PMTitleBar,    PMEnd,
  57.             PMItem("Quit"),    PM_UserData,    5,    PMEnd,
  58.             End;
  59.  
  60.         if(p) {
  61.             w=OpenWindowTags(NULL,    WA_IDCMP,    IDCMP_CLOSEWINDOW|IDCMP_MOUSEBUTTONS|IDCMP_MENUVERIFY,
  62.                     WA_DragBar,    TRUE,
  63.                     WA_Width,    150,
  64.                     WA_Height,    100,
  65.                     WA_Left,    150,
  66.                     WA_Top,        0,
  67.                     WA_Title,    "MenuVerify Demo",
  68.                     WA_CloseGadget,    TRUE,
  69.                     TAG_DONE);
  70.             if(w) {
  71.                 SetMenuStrip(w, &Intui_Menu);
  72.  
  73.                 while(r) {
  74.                     WaitPort(w->UserPort);                        // Wait for a message
  75.                     while((im=(struct IntuiMessage *)GetMsg(w->UserPort))) {    // Get the message
  76.                         CopyMem(im,&imsg,sizeof(struct IntuiMessage));        // Copy the contents of it
  77.  
  78.                         if(im->Class==IDCMP_MENUVERIFY) {            // Check if the user wants to see the menu
  79.                             if(im->MouseY>0 && im->MouseY<w->Height &&
  80.                                im->MouseX>0 && im->MouseX<w->Width) {        // Check if the mouse in our window
  81.                                
  82.                                im->Code=MENUCANCEL;                // Cancel the intuition menu
  83.                                
  84.                                imsg.Class=IDCMP_MOUSEBUTTONS;        // Make sure the menu gets opened...
  85.                                imsg.Code=MENUDOWN;                // (we fake a IDCMP_MOUSEBUTTONS message)
  86.                             }
  87.                         }
  88.  
  89.                         ReplyMsg((struct Message *)im);                // Reply the message
  90.  
  91.                         switch(imsg.Class) {
  92.                             case IDCMP_CLOSEWINDOW: r=FALSE; break;
  93.                             case IDCMP_MOUSEBUTTONS:            // The user has hit a mousebutton - time to open the menu!
  94.                                 if(imsg.Code==MENUUP ||
  95.                                    imsg.Code==MENUDOWN)    {        // Open only if the right (that is the right button, in this case :) ) button was hit
  96.                                     r=(BOOL)((ULONG)(PM_OpenPopupMenu(w,
  97.                                             PM_Menu,        p,
  98.                                             PM_Code,        imsg.Code,    // This will ensure that the user can change
  99.                                             TAG_DONE))-5);                // the time of apperance for the menu.
  100.                                 }
  101.                             break;
  102.                         }
  103.                     }
  104.                 }
  105.                 ClearMenuStrip(w);
  106.  
  107.                 CloseWindow(w);
  108.             } else printf("Window error!\n");
  109.             PM_FreePopupMenu(p);
  110.         } else printf("Menu error!\n");
  111.         CloseLibrary((struct Library *)PopupMenuBase);
  112.     }
  113. }
  114.